package net.lab1024.sa.admin.module.device.job;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.open.heartbeat.DeviceHeartbeatService;
import net.lab1024.sa.base.module.support.job.core.SmartJob;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 设备心跳检查，超过五分钟没有上报全部设置为离线
 */
@Slf4j
@Service
public class DeviceHeartbeatCheckJob implements SmartJob {

    private final DeviceHeartbeatService deviceHeartbeatService;

    public DeviceHeartbeatCheckJob(DeviceHeartbeatService deviceHeartbeatService) {
        this.deviceHeartbeatService = deviceHeartbeatService;
    }

    @Override
    public String run(String param) {
        CompletableFuture<String> future = deviceHeartbeatService.checkHeartbeat();
        try {
            return future.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // 标记线程中断状态
            throw new RuntimeException("线程中断", e);
        } catch (ExecutionException e) {
            log.error("设备心跳检查时出错", e);
            throw new RuntimeException(e);
        }
    }
}
